package controllers

import (
	"lsis/models"

	"github.com/astaxie/beego"
)

type resonpMsg struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
	Result  user   `json:"result"`
}

type user struct {
	Session string `json:"session"`
	Role    string `json:"role"`
}

type UserController struct {
	BaseController
}

func (c *UserController) URLMapping() {
	c.Mapping("regStaff", c.RegisterStaff)
	c.Mapping("update", c.UpdateInfo)

}

// @Title 申请美妆师
// @Description 用户通过扫描店铺的URL，申请为美妆师，头像需要先上传
// @Param   session   formData   string  true       "用户session"
// @Param   nickname  formData   string  true       "雇员昵称"
// @Param   store     formData   int     true       "店铺Id"
// @Param   icon      formData   string  false      "用户头像URL"
// @Success 200 {object} controllers.result
// @Failure 400
// @Failure 404
// @router /regStaff [post]
func (c *UserController) RegisterStaff() {

	icon := c.GetString("icon")
	storeId, err := c.GetInt("store")
	if err != nil {
		beego.Error("store id is invalid")
		c.returnResult(errCodeStore, "store Id invalid", "")
		return
	}

	nickname := c.GetString("nickname")

	err = models.RegisterStaff(c.user, nickname, icon, storeId)
	if err != nil {
		c.returnResult(errCodeStore, err.Error(), "")
	} else {
		c.returnResult(0, "success", "")
	}
}

// @Title 更新用户信息
// @Description 用户头像，昵称更新
// @Param   session   formData   string  true       "用户code"
// @Param   nickname  formData   string  false      "用户昵称"
// @Param   icon      formData   string  false      "用户头像URL"
// @Success 200 {object} controllers.result
// @Failure 400
// @Failure 404
// @router /update [post]
func (c *UserController) UpdateInfo() {

	var resp resonpMsg

	nickname := c.GetString("nickname")
	icon := c.GetString("icon")
	err := models.UpdateUserInfo(c.user.Id, nickname, icon)
	if err != nil {
		resp.Code = errCodeAddUser
		resp.Message = err.Error()
		beego.Error("update user failed:", err)
	} else {
		resp.Message = "success"
		resp.Code = 0
	}

	c.Data["json"] = &resp
	c.ServeJSON()
}
